import request from '../../utils/request';
import PublicService from '../../services/PublicService'
import ObserveManageService from '../../services/stationRule/observemanageService'
import {message} from 'antd';

export default {
  namespace: 'observeManage',
  state: {
    observeName: '',
    selectedRows: [], // 选中行
    inputChildren: [
      {
        type: 'input',
        label: `选项1`,
        paramName: 'select&&0',
        initialValue: '',
        span: 12,
        width: 210,
        rules: [],
        isAdd: true
        // addonAfter: '后缀'
      },
    ],
    length: 1,
    size: 10,
    page: 1,
    total: 20,
    loading: false,
    dataSource: [],
    isVisible: false,
    initData: {},
    isEdit:false,
  },
  effects: {

    // 添加行
    * addLine({payload}, {put, call, select}) {
      let inputChildren = yield select(state => state.observeManage.inputChildren);
      let length = yield select(state => state.observeManage.length);
      inputChildren.map(item => item.isAdd = false);
      inputChildren.push({
        type: 'input',
        label: `选项` + (length + 1),
        paramName: 'select&&' + length,
        initialValue: '',
        span: 12,
        width: 210,
        rules: [],
        isAdd: true
        // addonAfter: '后缀'
      });
      length++;
      yield put({type: 'saveInfo', payload: {inputChildren, length}});
    },
    // 删除行
    * deleteLine({payload}, {put, call, select}) {
      let inputChildren = yield select(state => state.observeManage.inputChildren);
      let children = inputChildren.filter(item => item.paramName !== payload.paramName);
      if (children.length < 1) {
        message.warn('不能删除');
        return
      }
      children[children.length - 1].isAdd = true;
      yield put({type: 'saveInfo', payload: {inputChildren: children}});
    },

    // 获取页面表格数据
    * getPageList({payload}, {put, call, select}) {
      yield put({type: 'saveInfo', payload: {loading: true}});
      let observeName = yield select(state => state.observeManage.observeName);
      let size = yield select(state => state.observeManage.size);
      let page = yield select(state => state.observeManage.page);
      let params = {observeName, size, page};
      let {ret} = yield call(ObserveManageService.getPageList, params);
      console.log(ret);
      try {
        yield put({type: 'save', payload: {dataSource: ret.rows, total: ret.total, loading: false}})
      } catch (e) {
        console.log(e)
      }

    },
    // 编辑和查看
    * editReadHandle({payload}, {call, put, select}) {
      let inputChildren = [];
      let data = payload.record;
      let array = data.selectItems.split(',');
      console.log(array);
      array.forEach((item, index) => {
        inputChildren.push({
          type: 'input',
          label: `选项` + (index + 1),
          paramName: 'select&&' + index,
          initialValue: item,
          span: 12,
          width: 210,
          rules: [],
          isAdd: false
        })
      });
      console.log(inputChildren);
      inputChildren[inputChildren.length - 1].isAdd = true;
      delete payload.record;
      yield put({type: 'saveInfo', payload: {...payload, inputChildren, length: array.length,initData:{observeName:data.observeName,id:data.id || ''}}});
    },
    // 删除函数
    * deleteHandle({payload}, {put, call, select}) {
      let selectRow = yield select(state => state.observeManage.selectedRows);
      if (selectRow.length === 0) {
        message.warn('未选择');
        return
      }
      let ids = selectRow.join(',');
      console.log(ids);
      let res = yield call(ObserveManageService.delete, {observeIds: ids});
      if (res.rc === 0) {
        message.success(res.ret)
      } else {
        message.error(res.err)
      }
      yield put({type: 'saveInfo', payload: {selectedRows: []}});
      yield put({type: 'getPageList'})
    },
    // page改变时候
    * pageChangeHandle({payload}, {put, call, select}) {
      yield put({type: 'saveInfo', payload});
      yield put({type: 'getPageList'})
    },

    // 区域空间
    * search({payload}, {put, call}) {
      yield put({type: 'saveInfo', payload: payload});
      yield put({type: 'getPageList'});
    },

    // 保存新增区域
    * submit({payload}, {put, call, select,}) {
      let data = payload.params;
      console.log(data);
      let selectedArray = [];
      for (let key in data) {
        if (key !== 'observeName') {
          selectedArray.push(data[key])
        }
      }
      let selects = selectedArray.join(',');
      console.log(selects);
      let initData = yield select(state => state.observeManage.initData);
      let res;
      if (initData.id) {
        res = yield call(ObserveManageService.update, {
          ...initData,
          selectItems: selects,
          observeName: data.observeName
        });
      } else {
        res = yield call(ObserveManageService.save, {selectItems: selects, observeName: data.observeName});
      }
      console.log(res);
      if (res.rc === 1) {
        message.error(res.err);
      }
      else {
        message.success(res.ret)
      }
      yield put({type: 'saveInfo', payload: {isVisible: false, page: 1, size: 10, initData: {}}});
      yield put({type: 'getPageList'})
    },

    * saveInfo({payload}, {put, call, take, select}) {
      let obj = PublicService.deepClone(payload);
      yield put({type: 'save', payload: {...obj}});
    }
  },
  reducers: {
    save(state, {payload}) {
      console.log(payload, 'reduce');
      return {...state, ...payload};
    },
  },
};
